      SUBROUTINE TOGMSH(x,y,xl,inc,NN,filer)
      
       IMPLICIT DOUBLE PRECISION (A-H,O-Z)     
      INCLUDE 'param_dim.inc'

      integer NNINC(ninc_max),NUMPI(ninc_max)
      integer NUMPCI(ninc_max),VARIA(ninc_max)
      real*8 C0,CINC(ninc_max)
!       COMMON N_Eq,NN,L,LEC,IMP
!       COMMON /incdata/ C0,CINC,N0,NINCL,NNINC,NPI
      COMMON /incdata/ C0,CINC,NNINC,NUMPI,NUMPCI,NINCL,VARIA

	
      CHARACTER*11 filer
      DIMENSION X(NX),Y(NX),xl(NX),INC(NX,2)
!       DIMENSION NNINC(4),CINC(4),NPI(4)

C     Abre o arquivo
      igeo= 14
      open(igeo,file=filer//'.geo',STATUS='UNKNOWN')

C     Calcula o numero total de Elementos (nos)
      
! calcula numero de elementos/nos do contorno externo
      naux=0
      do i=1,nincl
	  naux=naux+nninc(i)
      enddo
      n0=nn-naux   !no=numero de nohs do contorno externo


c     Calcula os comprimentos para gera�ao da malha associados aos n�s
      do i=1,nn
         xl(i)=0
      enddo

      do i =1,nn
         noi= inc(i,1)
         nof= inc(i,2)

         dltx= x(noi) - x(nof)
         dlty= y(noi) - y(nof)

         comp= sqrt(dltx*dltx + dlty*dlty)

         xl(noi)= xl(noi) + 0.5*comp 
         xl(nof)= xl(nof) + 0.5*comp 
      enddo

C imprime os nos e os comp caracteristicos.

      do i=1,nn
         write(igeo,1010) i,x(i),y(i),0.,xl(i)
 1010    format('Point(',i4,') = {',2(f16.8,','),f7.3,',',e12.4,'};')
      enddo

C     Imprime os elementos (Linhas)

      do i=1,nn
         write(igeo,1020) i,inc(i,1),inc(i,2)
 1020    format('Line(',i4,') = {',i4,',',i4,'};')         
      enddo


C     Imprime Loop Externo

      ie6= 1000000
      ill= nn+1
      
C     Imprime o inicio do Loop do contorno externo

      illc0=  ill

      write(igeo,1030) ie6+ill
 1030 format('Line Loop (',i7,') = {')

      
C     Imprime demais elementos do contorno externo
      write(igeo,1040) (i,i=1,n0-1)
 1040 format(10(i4,','))
         
      write(igeo,1050) n0
 1050 format(i4,' };')

                           
         
C     Define os contornos das inclusoes
      nf= n0
      ni= nf + 1
      
      do iinc=1,nincl
         
         ill= ill + 1
         nf= nf + nninc(iinc)

         write(igeo,1060) ie6+ill
 1060    format('Line Loop (',i7,') = {')
         
         if(iinc.lt.nincl) then
            write(igeo,1070) (i,i=ni,nf-1)
 1070       format(10(i4,','))
            write(igeo,1050) nf


c     Define a superficie da inclusao
            
            
            ni= ni + nninc(iinc)
         else
            write(igeo,1070) (i,i=ni,nf-1)
            write(igeo,1050) nf
c 1050       format(i4,' };')
         endif
         
      enddo

      
C     Define as superficies
      ill = illc0

c     Sup. externa
      write(igeo,1080) ill, ie6+ill,(ie6+ill+i,i=1,nincl-1)
 1080 format('Plane Surface (',i4,') = {',(i8,','))
      write(igeo,1090) ie6+ill+nincl
 1090 format(i8,'};')

c     Sup. das Inclusoes
      do i =1,nincl
         ill= ill+1
         write(igeo,1100) ill,ie6+ill
 1100    format('Plane Surface (',i4,') = {',i8,'};')
      enddo

***************************************************
C     Imprime as Linhas Fisicas (Physical Line)


      
C     Imprime Physical Line do  contorno externo

      ill= ill+1

      write(igeo,1110) ill
 1110 format('Physical Line (',i7,') = {')

C     Imprime elementos do contorno externo
      write(igeo,1040) (i,i=1,n0-1)
      write(igeo,1050) n0

                           
         
C   Imprime Physical Line dos contornos das inclusoes
      nf= n0
      ni= nf + 1
      
      do iinc=1,nincl
         
         ill= ill + 1
         nf= nf + nninc(iinc)

         write(igeo,1110) ie6+ill
c 1080    format('Physical Line (',i7,') = {')
         
         if(iinc.lt.nincl) then
            write(igeo,1070) (i,i=ni,nf-1)
            write(igeo,1050) nf
            
            ni= ni + nninc(iinc)
         else
            write(igeo,1070) (i,i=ni,nf-1)
            write(igeo,1050) nf
         endif
         
      enddo


C     Imprime Physical Surface's
      ill= ill + 1
      write(igeo,1120) (ill+i, illc0+i ,i=0,nincl)
 1120 format('Physical Surface(',i4,') = {',i4,'};')

      return
      end
